$(function(){
    var client_datagrid,client_search,client_form,client_dialog;
    client_datagrid = $("#client_datagrid");
    client_search =$("#searchbtn");
    client_form=$("#client_form");
    client_dialog=$("#dialog_edit");


    client_datagrid.datagrid({
        queryParams:{
            statusChange:2
        },
        url:'/sourceClient/list',
        fit:true,
        fitColumns:true,
        toolbar:'#datagrid_toolar',
        pagination:true,
        singleSelect:true,
        rownumbers:true,
        columns:[[

            {field:'realName',title:'客户姓名',width:100,align:'center'},
            {field:'tel',title:'手机号',width:100,align:'center'},
            {field:'qq',title:'QQ',width:100,align:'center'},
            {field:'address',title:'地址',width:100,align:'center'},
            {field:'clientType',title:'客户类型',width:100 ,formatter:clientTypeFormatter,align:'center'},
            {field:'idCardType',title:'证件类型',width:100, formatter:idCardTypeFormatter,align:'center'},
            {field:'idCardNumber',title:'证件号码',width:100,align:'center'},
            {field:'nation',title:'民族',width:100,align:'center'},
            {field:'nativePlace',title:'籍贯',width:100,align:'center'},
            {field:'marriage',title:'婚姻',width:60 ,formatter:marriageFormatter,align:'center'},
            {field:'inputEmployee',title:'录入人',width:100,formatter:inputEmployeeFormatter,align:'center'},
            {field:'princiEmployee',title:'负责人',width:100,formatter:princiEmployeeFormatter,align:'center'},
            {field:'inputTime',title:'注册时间',width:120,align:'center'},
            {field:'editTime',title:'编辑时间',width:120,align:'center'},
            {field:'status',title:'状态',width:100 ,formatter:stateFormatter,align:'center'},
            {field:'clientNumber',title:'客户代码',width:100,align:'center'},

        ]],


    });

    $("a[data-cmd]").on("click",function(){
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
    //对话框
    client_dialog.dialog({

        closed:true,
        buttons:'#dialog_check'

    })

    var cmdObj= {
        //吸纳
        client_add: function () {
            var rowData =client_datagrid.datagrid("getSelected");
            if(rowData){
                $.messager.confirm('温馨提示','您确定吸纳这个客户吗？',function(yes){
                    if(yes){

                        $.post("/sourceClient/attactClient?id="+rowData.id,function (data) {
                            if(data.success){

                                client_datagrid.datagrid("reload");
                                $.messager.alert("温馨提示",data.msg,"info",function () {

                                });
                            }else {
                                $.messager.alert("温馨提示",data.msg,"error");
                            }
                        },"json");
                    }
                });
            }else{
                $.messager.alert("温馨提示","请选择需要吸纳的客户","warning");
            }

        },



        //取消
        client_cancel:function(){
            client_dialog.dialog("cancel");
        },
        //刷新
        client_reload:function(){
            client_datagrid.datagrid("reload");
        },

    };


    //搜索
    client_search.textbox({
        buttonText:'SEARCH',
        prompt:'搜索关键字',
        label:'关键字',
        labelPosition:'left',
        labelWidth:50,
        width:300,
        onClickButton:function(){
            client_datagrid.datagrid("load",{
                keyword:client_search.val()
            });
        }
    });



});
function stateFormatter(value,record,index){
    if(value==0){
        return "<font color='#00CD66'>潜在客户</font>";
    }else if(value==1){
        return "<font color='#FA8072'>正式客户</font>";
    }else {
        return "<font color='#9B30FF'>资源池客户</font>";
    }
}
function marriageFormatter(value,record,index){
    if(value==0){
        return "已婚";
    }else if(value==1){
        return "未婚";
    }else{
        return "不明";
    }
}
function idCardTypeFormatter(value,record,index){
    if(value==0){
        return "护照";
    }else if(value==1){
        return "身份证";
    }
}
function clientTypeFormatter(value,record,index){
    if(value==1){
        return "个人";
    }else if(value==2){
        return "法人";
    }
}
function inputEmployeeFormatter(value,record,index){
    if(value){
        return value.name;
    }else{
        return "";
    }
}
function princiEmployeeFormatter(value,record,index){
    if(value){
        return value.name;
    }else{
        return "";
    }
}